package com.zhan.ielts.utils;

import com.zhan.frame.core.utils.DateUtils;
import com.zhan.frame.core.utils.NetUtil;
import com.zhan.ielts.domain.article.Article;
import com.zhan.ielts.domain.article.vo.HotArticleVo;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/19.
 */
public class GetArticle {
    public static String ARTICLE_TOKEN = "100-token";
    public static String GET_ARTICLE_CATEGORY = "http://m.service.zhan.com/auth/ieltsapp/categories/?access-token=";
    public static String GET_ARTICLE = "http://m.service.zhan.com/auth/ieltsapp/articles/?access-token=";
    public static String GET_DETAIL_ARTICLE = "http://m.service.zhan.com/auth/ieltsapp/articledetail/?access-token=";
    public static String GET_ALL_HOT_ARTICLE = "http://m.service.zhan.com/auth/ieltsapp/allhotarticles/?access-token=";
    public static String GET_HOT_ARTICLE = "http://m.service.zhan.com/auth/ieltsapp/hotarticles/?access-token=";

    public static void main(String[] args) {
        Long time = System.currentTimeMillis();
        for (int i = 1; i < 10; i++) {
            if (i != 2) {
//                List<Article> articleList = new ArrayList<>();
//                List<Article> str = getArticleList(articleList, i + "", 0, -1);
//                System.out.println(articleList.size());
            }
        }
        System.out.println(System.currentTimeMillis() - time);
    }

    public static String getCategoryIdList() {
        String categoryJson = null;
        try {
            String json = NetUtil.doGet(GET_ARTICLE_CATEGORY + ARTICLE_TOKEN);
            JSONObject sobj = JSONObject.fromObject(json);
                /* 读返回数据 */
            String code = sobj.getString("code");
            if (code.equals("200")) {
                categoryJson = sobj.getString("data");
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return categoryJson;
    }

    public static List<Article> getAllArticleList(List<Article> articleList, String categoryId, int offset, int totalNum) {
        StringBuilder urlSb = new StringBuilder(GET_ARTICLE + ARTICLE_TOKEN);
        urlSb.append("&catid=" + categoryId)
                .append("&offset=" + offset * 500)
                .append("&limit=500")
                .append("&end=").append(DateUtils.getNowDateLong());
        String json = NetUtil.doGet(urlSb.toString());

        JSONObject sobj = JSONObject.fromObject(json);
                /* 读返回数据 */
        String code = sobj.getString("code");
        if (code.equals("200")) {
            if (totalNum == -1) {
                totalNum = Integer.parseInt(sobj.getString("totalNum"));
            }
            JSONArray array = JSONArray.fromObject(sobj.getString("data"));
            List<Article> articles = JSONArray.toList(array, Article.class);
            articleList.addAll(articles);
            if (totalNum > 100) {
                totalNum -= 100;
                offset++;
                getAllArticleList(articleList, categoryId, offset, totalNum);
            }
        }
        return articleList;
    }

    /**
     * 拉取昨天更新的文章
     * @param articleList
     * @param categoryId
     * @param offset
     * @param totalNum
     * @return
     */
    public static List<Article> getArticleList(List<Article> articleList, String categoryId, int offset, int totalNum) {
        StringBuilder urlSb = new StringBuilder(GET_ARTICLE + ARTICLE_TOKEN);
        urlSb.append("&catid=" + categoryId)
                .append("&offset=" + offset * 100)
                .append("&limit=100")
                .append("&start=").append(DateUtils.getYesterDayDateLong())
                .append("&end=").append(DateUtils.getNowDateLong());
        String json = NetUtil.doGet(urlSb.toString());

        JSONObject sobj = JSONObject.fromObject(json);
                /* 读返回数据 */
        String code = sobj.getString("code");
        if (code.equals("200")) {
            if (totalNum == -1) {
                totalNum = Integer.parseInt(sobj.getString("totalNum"));
            }
            JSONArray array = JSONArray.fromObject(sobj.getString("data"));
            List<Article> articles = JSONArray.toList(array, Article.class);
            articleList.addAll(articles);
            if (totalNum > 100) {
                totalNum -= 100;
                offset++;
                getArticleList(articleList, categoryId, offset, totalNum);
            }
        }
        return articleList;
    }


    /**
     * 得到所有热门文章
     * @return
     */
    public static List<HotArticleVo> getAllHotArticleList() {
        List<HotArticleVo> articleList = new ArrayList<>();
        StringBuilder urlSb = new StringBuilder(GET_ALL_HOT_ARTICLE + ARTICLE_TOKEN);
        String json = NetUtil.doGet(urlSb.toString());

        JSONObject sobj = JSONObject.fromObject(json);
                /* 读返回数据 */
        String code = sobj.getString("code");
        if (code.equals("200")) {
            JSONArray array = JSONArray.fromObject(sobj.getString("data"));
            List<HotArticleVo> articles = JSONArray.toList(array, HotArticleVo.class);
            articleList.addAll(articles);
        }
        return articleList;
    }

    /**
     * 得到当天的文章
     * @return
     */
    public static List<HotArticleVo> getHotArticleList() {
        List<HotArticleVo> articleList = new ArrayList<>();
        StringBuilder urlSb = new StringBuilder(GET_HOT_ARTICLE + ARTICLE_TOKEN);
        String json = NetUtil.doGet(urlSb.toString());

        JSONObject sobj = JSONObject.fromObject(json);
                /* 读返回数据 */
        String code = sobj.getString("code");
        if (code.equals("200")) {
            JSONArray array = JSONArray.fromObject(sobj.getString("data"));
            List<HotArticleVo> articles = JSONArray.toList(array, HotArticleVo.class);
            articleList.addAll(articles);
        }
        return articleList;
    }



    public static Article getArticleDetail(String articleId) {
        StringBuilder urlSb = new StringBuilder(GET_DETAIL_ARTICLE + ARTICLE_TOKEN);
        urlSb.append("&articleid=" + articleId);
        String json = NetUtil.doGet(urlSb.toString());
        json = json.replace("catId", "categoryId");

        JSONObject sobj = JSONObject.fromObject(json);
                /* 读返回数据 */
        String code = sobj.getString("code");
        Article article = null;
        if (code.equals("200")) {
            JSONObject array = JSONObject.fromObject(sobj.getString("data"));
            article = (Article) JSONObject.toBean(array, Article.class);
        }
        return article;
    }
}
